package com.example.chooose;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class add extends AppCompatActivity {
    private SharedPreferences sp;
    private TextView tv_name,tv_time,tv_teacher;
    private List<Map<String,String>> list;
    private MyAdapter adapter;
    private SharedPreferences.Editor editor;
    private ListView lv_user;
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        sp = getSharedPreferences("user",MODE_PRIVATE);
        editor = sp.edit();

        lv_user = (ListView) findViewById(R.id.lv_user);
        list = new ArrayList<>();
        getData();
        adapter = new MyAdapter(
                this,
                list,
                R.layout.item,
                new String[]{"name","time","teacher"},
                new int[]{R.id.tv_name,R.id.tv_time,R.id.tv_teacher}
        );

        lv_user.setAdapter(adapter);

        lv_user.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                MySqlite mySQLite = new MySqlite(add.this, 1);
                db= mySQLite.getWritableDatabase();
                //使用ContentValues添加数据
                ContentValues values=new ContentValues();
                values.put("name",list.get(i).get("name"));
                values.put("time",list.get(i).get("time"));
                values.put("teacher",list.get(i).get("teacher"));
                db.insert("huo", null, values);
                db.close();
                Toast.makeText(add.this,"选课成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        getData();
        adapter = new MyAdapter(
                this,
                list,
                R.layout.item,
                new String[]{"name","time","teacher"},
                new int[]{R.id.tv_name,R.id.tv_time,R.id.tv_teacher}
        );

        lv_user.setAdapter(adapter);
    }



    private class MyAdapter extends SimpleAdapter {
        public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource,
                         String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            tv_name = v.findViewById(R.id.tv_name);
            tv_time = v.findViewById(R.id.tv_time);
            tv_teacher = v.findViewById(R.id.tv_teacher);
            tv_name.setText(list.get(position).get("name"));
            tv_teacher.setText(list.get(position).get("teacher"));
            tv_time.setText(list.get(position).get("time"));
            return v;
        }
    }

    public List<Map<String,String>> getData(){

        Map<String,String> map = new HashMap<>();
        map.put("name","C语言");
        map.put("time","18课时");
        map.put("teacher","杨过");
        list.add(map);

        Map<String,String> map1 = new HashMap<>();
        map1.put("name","大数据");
        map1.put("time","20课时");
        map1.put("teacher","小龙女");
        list.add(map1);

        Map<String,String> map2 = new HashMap<>();
        map2.put("name","数据结构");
        map2.put("time","15课时");
        map2.put("teacher","张三");
        list.add(map2);

        Map<String,String> map3 = new HashMap<>();
        map3.put("name","计算机基础");
        map3.put("time","8课时");
        map3.put("teacher","李四");
        list.add(map3);
        return list;
    }
}
